package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.example.demo.entity.Customer;
import com.example.demo.entity.Memorandum;
import com.example.demo.entity.OrderBase;
import com.example.demo.repository.MemorandumRepository;
import com.example.demo.service.MemorandumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MemorandumController {

    @Autowired
    MemorandumService memorandumService;

    @GetMapping("/memorandumFind")
    public Map<String,Object> memorandumFind(@RequestParam("id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        Memorandum memorandum = memorandumService.getMemorandum(id);
        dataMap.put("status","0");
        dataMap.put("data",memorandum);
        return dataMap;
    }
    /*
    *   注意提醒的格式需要是：1235644456123
    *
    * */
    @GetMapping("/memorandum")
    public Map saveMemorandum(@RequestParam Map memorandum, HttpServletRequest request,HttpSession session){
        Map dataMap = new HashMap();
        Memorandum save = memorandumService.saveMemorandum(memorandum,request);
        dataMap.put("status",0);
        dataMap.put("data",save);
        return dataMap;
    }

    /*
    *  这是分页查询订单数量
    *   parms => page:第一个页
    *            size:要几条
    *            memorandumQuery:备忘录里面的参数集合(id.orderName)
    * */
    @GetMapping("/MemorandumQuery")
    public Map<String,Object> MemorandumQuery(@RequestParam Map memorandumQuery,HttpSession session){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        JSONObject memorandumJson = (JSONObject) JSONObject.parse(memorandumQuery.get("memorandumQuery").toString());
        String userId = session.getAttribute("user").toString();
        Page<Memorandum> datas = memorandumService.findMemorandumCriteria(Integer.valueOf(memorandumQuery.get("page").toString()), Integer.valueOf(memorandumQuery.get("size").toString()),memorandumJson,userId);
        dataMap.put("data", JSON.parse(JSON.toJSONString(datas, SerializerFeature.DisableCircularReferenceDetect)));
        dataMap.put("status",0);
        return dataMap;
    }

    /*
    *  这是通过备忘录ID删除指定的备忘录
    *   parms => id:要删除的ID
    * */
    @GetMapping("/memorandumDelete")
    public Map<String,Object> memorandumDelete( @RequestParam(value = "id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null ){
            dataMap.put("errMsg","无效的ID");
            dataMap.put("status",0);
        }else{
            Boolean dataBool = memorandumService.memorandumDelete(id);
            dataMap.put("data",dataBool);
            dataMap.put("status",0);
        }
        return dataMap;
    }

    /*
   *  确认收货
   *   parms => id:订单ID
   * */
    @PostMapping("/memorandumDone")
    public Map<String,Object> memorandumDone(@RequestParam(value = "id") Integer id){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        if( id == null){
            dataMap.put("message","参数不对！");
            dataMap.put("status",20);
        }else{
            memorandumService.memorandumDone(id);
            dataMap.put("message","收货成功！");
            dataMap.put("status",0);
        }
        return dataMap;
    }

    

}
